package com.yanqu.road.dao.impl.travelbook;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.travelbook.TravelBookUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author : Cxp
 * @Time : 2024-12-31
 * @Description : 旅行册数据Dao
 */

public class TravelBookUserDataDaoImpl extends TempDataObjectDao<TravelBookUserData> {
    public static String getAlbumSql = "select * from t_u_travel_book_data where userId=?";

    public TravelBookUserData getUserData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getAlbumSql);
            int i = 1;
            call.setLong(i, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }

        return null;
    }

    public static String updateAlbumSql = "update `t_u_travel_book_data` set guideIdx=?, onceRecycle=? where userId=?";

    @Override
    protected boolean updateData(TravelBookUserData userData) {
        userData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, userData.getGuideIdx()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userData.getOnceRecycle()));
        para.put(i, new DbParameter(Types.BIGINT, userData.getUserId()));
        return getDb().executeNoneQuery(updateAlbumSql, para) > 0;
    }

    public static String insertAlbumSql = "insert into `t_u_travel_book_data`(userId,guideIdx,onceRecycle) values (?,?,?)";

    @Override
    protected boolean addData(TravelBookUserData userData) {
        userData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BIGINT, userData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userData.getGuideIdx()));
        para.put(i, new DbParameter(Types.BOOLEAN, userData.getOnceRecycle()));
        return getDb().executeNoneQuery(insertAlbumSql, para) > 0;
    }

    @Override
    protected TravelBookUserData parseData(ResultSet rs) throws SQLException {
        TravelBookUserData userData = new TravelBookUserData();

        userData.setUserId(rs.getLong("userId"));
        userData.setGuideIdx(rs.getInt("guideIdx"));
        userData.setOnceRecycle(rs.getBoolean("onceRecycle"));

        return userData;
    }
}
